<template>
    <div class="bg">
      <div class="login_part">
        <el-row type="flex" class="row-bg" justify="center">
            <el-col :span="24">
                <div class="tital">一步充电后台管理系统</div>
            </el-col>
        </el-row>
        <el-row type="flex" class="row-bg" justify="center">
            <el-col :span="18">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                    <el-form-item prop="username">
                        <el-input style="border-radius: 8px;" v-model="ruleForm.username" placeholder="username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input style="border-radius: 8px;" type="password" placeholder="password" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                    </el-form-item>
                    <div class="login-btn">
                        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    </div>
                    <p style="font-size:12px;line-height:30px;color:#999;">Tips : 忘记密码请联系管理员。</p>
                </el-form>
            </el-col>
        </el-row>
      </div>
    </div>
</template>

<script>
import { loginByUsername } from "../../api/login";
import { setToken } from "../../util/auth"

export default {
  data: function() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const self = this;
      self.$refs[formName].validate(valid => {
        if (valid) {
          loginByUsername(self.ruleForm.username,self.ruleForm.password).then(function(resp) {
            if(resp.errmsg === 'ok'){
                setToken(resp.data);
                self.$router.push({ path: '/index' });
            }
          }).catch(function(e){
              console.log(e);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.tital {
  text-align: center;
  margin-top:15%;
  font-size: 26px;
  margin-bottom: 6%;
  color:#2882fe;
  font-weight: 600;
  letter-spacing: 3px;
  font-size: 32px;
}

.login-btn button {
  width: 100%;
  background: #2882fe;
  border-radius: 8px;
  font-size: 20px;
  padding: 20px;
}
.login-btn button:hover {
  width: 100%;
  background: #1f6ad3;
  border-radius: 8px;
  font-size: 20px;
  padding: 20px;
}
.login_part{
  background: #fff;
    width: 500px;
    left: 37%;
    position: absolute;
    top:28%;
    border-radius: 22px;
    box-shadow: 1px 1px 5px #b9b9b9;
    padding-bottom: 4%;
}

</style>